/**
 * Copyright (C) 2010  Hylke van der Schaaf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package hextd.map;

import com.twolattes.json.Value;
import hextd.tower.FactoryTower.type;
import hextd.wave.PathNormal;
import hextd.wave.Wave;
import java.util.ArrayList;
import java.util.List;
import com.usoog.tdcore.gamestate.GameState;
import com.usoog.tdcore.map.GameMap;
import com.usoog.tdcore.pathfinder.Pathfinder;
import hextd.core.GameGridImplementation;
import hextd.core.GameStateImplementation;
import hextd.core.PlayerAiWaveImp;
import hextd.core.PlayerImp;

/**
 *
 * @author hylke
 */
public class GameMapImplementation implements GameMap {

	private boolean initialised = false;
	@Value
	public String title = "noName";
	@Value(optional = true)
	public String info = ""
			+ "Author: <b>The Unknown Mapper</b>";
	@Value(optional = true)
	public String story = "";
	@Value(optional = true)
	public String storyWin = "";
	@Value(optional = true)
	public String storyLos = "";
	@Value(optional = true)
	public String backGroundImage = "";
	@Value(optional = true)
	public int orientation = 0;
	@Value
	public List<String> gridData;
	@Value
	public List<JsonPlayer> players;
	@Value(optional = true)
	public JsonPlayerAiWave waveCaster;
	@Value
	public List<PathNormal> paths;
	@Value
	public List<Wave> waves;
	@Value(optional = true)
	public List<type> notTowers;
	@Value(optional = true)
	public List<JsonTowerOptions> towerOptions;

	public GameMapImplementation() {
	}

	@Override
	public Status getStatus() {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public void loadIntoGameState(GameState target) {

		GameStateImplementation gs = (GameStateImplementation) target;
		GameGridImplementation gameGrid = (GameGridImplementation) target.getGameGrid();

		gs.setMapLoading(true);
		gs.resetTowerOptions();
		if (towerOptions != null) {
			for (JsonTowerOptions options : towerOptions) {
				gs.setTowerOptions(options.type, options);
			}
		}

		gs.setLevelTitle(getTitle());
		gs.setLevelInfo(getInfo());
		gs.setBackgroundImage(getBackGroundImage());

		gameGrid.fillWith(getGridData());

		List<Pathfinder> gsPaths = gs.getPaths();
		gsPaths.clear();
		gsPaths.addAll(paths);
		for (PathNormal p : paths) {
			p.setGameState(target);
			p.finalise();
		}

		for (JsonPlayer p : players) {
			PlayerImp player = new PlayerImp(-1, target, p.lives, p.wallet);
			player.setInterest(p.interestStart);
			player.setInterestInc(p.interestIncrease);

			gs.addPlayer(player);
			for (JsonTower t : p.towers) {
				t.loadTower(player, gs);
			}

		}

		if (waveCaster != null) {
			PlayerAiWaveImp player = new PlayerAiWaveImp(-1, target, waveCaster.waves);
			gs.addPlayer(player);
		}
		gs.setMapLoading(false);
	}

	public void initialise() {
		if (waves == null) {
			waves = new ArrayList<Wave>();
		}
		if (paths == null) {
			paths = new ArrayList<PathNormal>();
		}
		int level = 1;
		for (Wave wave : waves) {
			wave.setLevel(level++);
		}
		initialised = true;
	}

	public List<String> getGridData() {
		if (gridData == null) {
			gridData = new ArrayList<String>();
		}
		return gridData;
	}

	public List<PathNormal> getPaths() {
		if (paths == null) {
			paths = new ArrayList<PathNormal>();
		}
		return paths;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getTitle() {
		return title;
	}

	public void setInfo(String info) {
		this.info = info;
	}

	public String getInfo() {
		return info;
	}

	public void setBackGroundImage(String backGroundImage) {
		this.backGroundImage = backGroundImage;
	}

	public String getBackGroundImage() {
		return backGroundImage;
	}
}
